﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mimoza.ObjectLayer;

namespace Mimoza.Apps.Console.Commands
{
    public partial class NetworkSettingsForm : Form
    {
        private Host m_host;
        private AdapterInfo[] m_adaptersInfo;

        private void UpdateForm()
        {

        }

        public NetworkSettingsForm(Host host)
        {
            InitializeComponent();
            m_host = host;
            m_adaptersInfo = host.AdaptersInfo;
            if (null == m_adaptersInfo)
            {
                m_adaptersInfo = new AdapterInfo[1];
                m_adaptersInfo[0] = new AdapterInfo();
                m_adaptersInfo[0].AdapterID = 0;
                m_adaptersInfo[0].EnableDHCP4IP = true;
                m_adaptersInfo[0].EnableDHCP4DNS = true;
            }

            this.radioButtonAutoIP.Checked = m_adaptersInfo[0].EnableDHCP4IP;
            this.radioButtonStaticIP.Checked = !m_adaptersInfo[0].EnableDHCP4IP;
            this.ipAddress.Enabled = !m_adaptersInfo[0].EnableDHCP4IP;
            this.subnetMask.Enabled = !m_adaptersInfo[0].EnableDHCP4IP;
            this.defaultGateway.Enabled = !m_adaptersInfo[0].EnableDHCP4IP;

            this.ipAddress.Text = m_adaptersInfo[0].IPAddress;
            this.subnetMask.Text = m_adaptersInfo[0].SubNetMask;
            this.defaultGateway.Text = m_adaptersInfo[0].DefaultGateWay;
            
        }

        private void radioButtonAutoIP_CheckedChanged(object sender, EventArgs e)
        {
            m_adaptersInfo[0].EnableDHCP4IP = this.radioButtonAutoIP.Checked;
            this.radioButtonStaticIP.Checked = !m_adaptersInfo[0].EnableDHCP4IP;
            this.ipAddress.Enabled = !m_adaptersInfo[0].EnableDHCP4IP;
            this.subnetMask.Enabled = !m_adaptersInfo[0].EnableDHCP4IP;
            this.defaultGateway.Enabled = !m_adaptersInfo[0].EnableDHCP4IP;
        }

        private void bOK_Click(object sender, EventArgs e)
        {
            m_adaptersInfo[0].EnableDHCP4IP = this.radioButtonAutoIP.Checked;
            if (!m_adaptersInfo[0].EnableDHCP4IP)
            {
                m_adaptersInfo[0].IPAddress = this.ipAddress.Text;
                m_adaptersInfo[0].SubNetMask = this.subnetMask.Text;
                m_adaptersInfo[0].DefaultGateWay = this.defaultGateway.Text;
            }
            else 
            {
                m_adaptersInfo[0].IPAddress = "";
                m_adaptersInfo[0].SubNetMask = "";
                m_adaptersInfo[0].DefaultGateWay = "";

            }

            m_host.AdaptersInfo = m_adaptersInfo;
            this.DialogResult = DialogResult.OK;
        }
    }
}
